/*
 * @(#)IncrementalBackPropagation.java        1.0 2000/05/09
 *
 * This file is part of Xfuzzy 3.0, a design environment for fuzzy logic
 * based systems.
 *
 * (c) 2000 IMSE-CNM. The authors may be contacted by the email address:
 *                    xfuzzy-team@imse.cnm.es
 *
 * Xfuzzy is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation.
 *
 * Xfuzzy is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
//ALGORITMO DE RETROPROPAGACION DE ERRORES		//
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//

package xfuzzy.xfsl.model.algorithm;

import xfuzzy.xfsl.model.*;
import xfuzzy.lang.*;
import xfuzzy.xfds.XfdsDataSet;

/**
 * Algoritmo de Retropropagaci�n de errores incremental
 * 
 * @author Francisco Jos� Moreno Velo
 *
 * @see "Rumelhart, D. E., Hinton, G. E., Williams, R. J., 
 * Learning representations by back-propagation, Nature, Vol. 323, pp. 533-536, 1986"
 */
public class IncrementalBackPropagation extends XfslGradientBasedAlgorithm {

	//----------------------------------------------------------------------------//
	//                            MIEMBROS PRIVADOS                               //
	//----------------------------------------------------------------------------//

	/**
	 * Factor de aprendizaje
	 */
	private double rate;
	
	//----------------------------------------------------------------------------//
	//                                CONSTRUCTOR                                 //
	//----------------------------------------------------------------------------//

	/**
	 * Constructor
	 */
	public IncrementalBackPropagation() {
		this.rate = 0.1;
	}

	//----------------------------------------------------------------------------//
	//                             M�TODOS P�BLICOS                               //
	//----------------------------------------------------------------------------//

	/**
	 * Devuelve el c�digo de identificaci�n del algoritmo
	 */
	public int getCode() {
		return INCREMENTALBACKPROP;
	}

	/**
	 * Actualiza los par�metros de configuraci�n del algoritmo
	 */
	public void setParameters(double[] param) throws XflException {
		if(param.length != 1) throw new XflException(26);
		rate = super.test(param[0], POSITIVE);
	}

	/**
	 * Obtiene los par�metros de configuraci�n del algoritmo
	 */
	public XfslAlgorithmParam[] getParams() {
		XfslAlgorithmParam[] pp = new XfslAlgorithmParam[1];
		pp[0] = new XfslAlgorithmParam(rate, 0.1, POSITIVE, "Learning Rate");
		return pp;
	}

	/**
	 * Ejecuta una iteraci�n del algoritmo 
	 */
	public XfslEvaluation iteration(Specification spec, XfdsDataSet dataset,
			XfslErrorFunction ef) throws XflException {
		
		shufflePattern(dataset);
		XfslEvaluation prev = ef.evaluate(spec,dataset,1.0);
		Parameter[] param = spec.getAdjustable();
		for(int p=0; p<dataset.input.length; p++) {
			computeErrorGradient(spec,dataset.getSingle(p),ef);
			for(int i=0; i<param.length; i++) {
				param[i].setDesp(-rate*param[i].getDeriv());
				param[i].setDeriv(0);
			}
			spec.update();
		}
		return ef.evaluate(spec,dataset,prev.error);		
	}
	
	 //-------------------------------------------------------------//
	 // Cambia el orden de los patrones de forma aleatoria		//
	 //-------------------------------------------------------------//

	 private void shufflePattern(XfdsDataSet dataset) {
	  for(int i=0; i<dataset.input.length; i++)
	   for(int j=i+1; j<dataset.input.length; j++)
	    if(Math.random()<0.5) {
	     double[] aux = dataset.input[i];
	     dataset.input[i] = dataset.input[j];
	     dataset.input[j] = aux;
	     aux = dataset.output[i];
	     dataset.output[i] = dataset.output[j];
	     dataset.output[j] = aux;
	    }
	 }

}
